package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.common.Result;
import cn.edu.lingnan.entity.Contest;
import cn.edu.lingnan.entity.dto.SelectLevelDTO;
import cn.edu.lingnan.entity.excelPojo.StudentContest;
import cn.edu.lingnan.mapper.ContestMapper;
import cn.edu.lingnan.mapper.StudentContestMapper;
import cn.edu.lingnan.service.ExcelService;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExcelServiceImpl extends ServiceImpl<StudentContestMapper, StudentContest> implements ExcelService {


    private static final String filePath = System.getProperty("user.dir") + File.separator+"files"+File.separator;
    /**
     * 选中评级
     * @param dto
     * @return
     */
    @Override
    public Result selectLevel(SelectLevelDTO dto) {
        if (ObjectUtil.isEmpty(dto.getIds())){
            return Result.error("没有数据选中,请先选中数据");
        }
        //判断完集合是否为空后
        //进行数据选中
        List<StudentContest> list = lambdaQuery().in(StudentContest::getId, dto.getIds())
                .list()
                .stream()
                .filter(l->!ObjectUtil.contains(l.getPriceLevel(),dto.getLevel()))
                .collect(Collectors.toList());
        list.forEach(l->l.setPriceLevel("("+dto.getLevel()+")"+l.getPriceLevel()));
        boolean b = updateBatchById(list);
        return b?Result.success():Result.error("修改失败");
    }
    /**
     * 获取文件
     *
     * @param
     * @param
     */
    @Override
    public void getModle(HttpServletResponse response) {
        OutputStream os = null;
        try {
            String fullPath = filePath + "导入模板.xlsx";
            System.out.println("Attempting to read file from: " + fullPath); // 输出调试信息

            // 检查文件是否存在
            File file = new File(fullPath);
            if (!file.exists()) {
                throw new FileNotFoundException("文件不存在: " + fullPath);
            }

            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("导入模板.xlsx", "UTF-8"));
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");


            byte[] bytes = FileUtil.readBytes(fullPath);
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace(); // 输出完整的异常信息
            System.out.println("文件下载失败: " + e.getMessage());
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
